#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdlib.h>
#include<errno.h>
#include<ctype.h>
#include<getopt.h>
#include<arpa/inet.h>

void print_usage(char *progname)
{
         printf("%s   usage:\n",progname);
	 printf("-p(--port):server listen port\n");
	 printf("-h(--help):print this help information.\n");
	 return;
}



int main(int argc,char *argv[])
{
	int                  server_fd=-1;
        int                  client_fd;
        struct sockaddr_in   serveraddr;
	struct sockaddr_in   clientaddr;
        pid_t                pid;
	int                  rv=-1;
	int                  port=0;
	socklen_t            len;
	int                  ch;
	int                  on=1;
	struct option      opts[]={
	
	
		
	    		{"port",required_argument,NULL,'p'},
			{"help",no_argument,NULL,'h'},
			{NULL,0,NULL,0}
	
	
	};
	while((ch=getopt_long(argc,argv,"p:h",opts,NULL))!=-1)
	{
	
		switch(ch)
		{
			case'p':
				port=atoi(optarg);
				break;
			case'h':
				print_usage(argv[0]);
				return 0;
		}

	}
	if(!port)
	{
	
		print_usage(argv[0]);
		return   0;
	}
	server_fd=socket(AF_INET,SOCK_STREAM,0);
		if(server_fd<0)
		{
		
			printf("create socket failure:%s\n",strerror(errno));
			return    -1;
		
		}
	printf("create socket [%d] sucessful",server_fd);
	setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
	memset(&serveraddr,0,sizeof(serveraddr));
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(port);
        serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);//监听全部的IP地址
	rv=bind(server_fd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
       		 if(rv<0)
		{
	
			printf("socket[%d] bind on port [%d] failure:%s\n",server_fd,port,strerror(errno));
			return    -2;
		}
	 listen(server_fd,13);
        	printf("start to listen port[%d]\n",strerror(errno));
	 while(1)
	 {
	 
		 printf("start accept client incoming...\n");
		 client_fd=accept(server_fd,(struct sockaddr*)&clientaddr,&len);
		 if(client_fd<0)
		 {
		 
			 printf("accept new client failure:%s\n",strerror(errno));
			 continue;
		 }
		 printf("accept new client[%s:%d] successful\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
		pid=fork();
		if(pid<0)
		{
		
			printf("create child process failure:%s",strerror(errno));
			close(client_fd);
			continue;
		}
		else if(pid>0)
		{
		
			printf("parent process[%d] start running,child process is[%d]",getpid(),pid);
			close(client_fd);
			continue;
		}
		else if(pid==0)
		{
		
			printf("create child process[%d] sucessful\n,parent process is pid[%d]\n",getpid(),getppid());
			close(server_fd);
			char          buf[1024];
			int           i;
			while(1)
			{
			
				memset(buf,0,sizeof(buf));
				rv=read(client_fd,buf,sizeof(buf));
				if(rv<0)
				{
				
					printf("read data from client socketfd[%d] failure:%s\n ",client_fd,strerror(errno));
					close(client_fd);
					exit(0);

				}
				else if (rv==0)
				
				{
					printf("socket[%d] get disconnect\n",client_fd);
					close(client_fd);
					exit(0);

				}
				else  if(rv>0)
				{
				
					printf("read %d bytes data from server:%s\n",rv,buf);
				}
				for(i=0;i<rv;i++)
				{
					buf[i]=toupper(buf[i]);
				}
				rv=write(client_fd,buf,rv);
				if(rv<0)
				{
				
					printf("write to client by socketfd[] failure:%s\n",client_fd,strerror(errno));
					close(client_fd);
					exit(0);
				}




			}
		}

	 }
	 close(server_fd);
	 return   0;


        

}
